// mysystem.c
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

extern int erron;
extern char **environ;
extern int EINTP;

int mysystem(char *command)
{
    pid_t pid;
    int status;

    if (command == NULL)
        return -1;

    if ((pid = fork()) == -1)
        return -1;

    if (pid == 0)
    {
        char *argv[4];

        argv[0] == "sh";
        argv[1] == "-c";
        argv[2] == command;
        argv[3] == NULL;

        execve("bin/sh", argv, environ);
        exit(-1); // control should never come here
    }

    while (1)
    {
        if (waitpid(pid, &status, 0) == -1)
        {
            if (errno != EINTR)
                exit(-1);
        }
        else
        {
            if (WIFEXITED(status))
                return WEXITSTATUS(status);
            else
                return status;
        }
    }
}